package com.ushine.design.design.c_behavioral_pattern.f_memento.c_extend;


import com.ushine.design.design.c_behavioral_pattern.f_memento.b_example.Chessman;

public class Client {
    private static int index = -1;
    private static MementoCaretaker mc = new MementoCaretaker();

    public static void main(String[] args) {
        Chessman chessman = new Chessman("车", 1, 1);
        play(chessman);
        chessman.setY(4);
        play(chessman);
        chessman.setX(5);
        play(chessman);
        undo(chessman, index);
        undo(chessman, index);
        redo(chessman, index);
//		chessman.setX(16);
//		play(chessman);
//		undo(chessman, index);
        redo(chessman, index);
    }

    /**
     * 下棋
     *
     * @param chessman
     */
    public static void play(Chessman chessman) {
        mc.setMementos(chessman.save());
        index++;
        System.out.println(
                "棋子" + chessman.getLabel() + "当前位置为：" + "第" + chessman.getX() + "行" + "第" + chessman.getY() + "列");
    }

    /**
     * 悔棋
     *
     * @param chessman
     * @param i
     */
    public static void undo(Chessman chessman, int i) {
        System.out.println("******悔棋*****");
        index--;
        chessman.resore(mc.getMementos(i - 1));
        System.out.println(
                "棋子" + chessman.getLabel() + "当前位置为：" + "第" + chessman.getX() + "行" + "第" + chessman.getY() + "列");
    }

    /**
     * 撤销悔棋
     *
     * @param chessman
     * @param i
     */
    public static void redo(Chessman chessman, int i) {
        System.out.println("******撤销悔棋****");
        index++;
        chessman.resore(mc.getMementos(i + 1));
        System.out.println(
                "棋子" + chessman.getLabel() + "当前位置为：" + "第" + chessman.getX() + "行" + "第" + chessman.getY() + "列");
    }
}
